package rs.mbs2.epics.server.session.dao.bean;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.Query;

import rs.mbs2.epics.server.entity.ProfileImage;
import rs.mbs2.epics.server.session.dao.localinterface.ProfileImageDaoLocal;

@Stateless
@Local(ProfileImageDaoLocal.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ProfileImageDaoBean extends GenericDaoBean<ProfileImage, Integer> implements ProfileImageDaoLocal {

	@SuppressWarnings("unchecked")
	@Override
	public ProfileImage findByPath(String path) {
		Query q = em.createNamedQuery("findByPath");
		q.setParameter("location", path);
		List<ProfileImage> results = q.getResultList();
		
		if(results.isEmpty())
			return null;
		
		return results.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ProfileImage findByUserId(Integer id) {
		Query q = em.createNamedQuery("findByUserId");
		q.setParameter("user_id", id);
		List<ProfileImage> results = q.getResultList();
		
		if(results.isEmpty())
			return null;
		
		return results.get(0);
	}

}
